import { createRouter, createWebHashHistory } from "vue-router";
import { LoadBasic } from '#/go/server/Handler.js';
import { useAppStore } from '@/store/app.js'; // 引入 useAppStore
import { ElLoading } from 'element-plus';

const routes = [
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/login.vue"),
    }, {
        path: "/",
        name: "index",
        component: () => import("@/views/index.vue"),
        redirect: "/basic",
        children: [
            {
                path: "/basic",
                name: "basic",
                component: () => import("../views/basic/index.vue"),
            }, {
                path: "/burn",
                name: "burn",
                component: () => import("../views/burn/index.vue"),
            }, {
                path: "/logs",
                name: "logs",
                component: () => import("../views/logs/index.vue"),
            }, {
                path: "/sc_test",
                name: "sc_test",
                component: () => import("../views/sc_test/index.vue"),
            }
        ]
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach(async (to, from, next) => {
    // if (to.path === "/burn") {
    //     const appStore = useAppStore();
    //     if (appStore.state.lines.length === 0) {
    //         const loadingInstance = ElLoading.service({
    //             text: "加载基础数据..."
    //         })
    //         await LoadBasic().then(async res => {
    //             console.log(res)
    //             useAppStore().setLines(res.line); // 存储 lines 到 app 状态
    //             useAppStore().setProduct(res.product); // 存储 product 到 app 状态
    //         }).finally(() => {
    //             loadingInstance.close()
    //         })
    //     }
    // }
    return next();
})

export default router;
